package com.example.projectmanagement.repository.study;

import com.example.projectmanagement.model.study.StudyPointsTransaction;
import com.example.projectmanagement.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface StudyPointsTransactionRepository extends JpaRepository<StudyPointsTransaction, Long> {
    Page<StudyPointsTransaction> findByUser(User user, Pageable pageable);
    List<StudyPointsTransaction> findByUserAndTransactionTimeBetween(User user, LocalDateTime start, LocalDateTime end);
    
    @Query("SELECT SUM(spt.points) FROM StudyPointsTransaction spt WHERE spt.user.id = :userId")
    Integer getTotalPointsByUserId(@Param("userId") Long userId);
    
    @Query("SELECT spt FROM StudyPointsTransaction spt WHERE spt.user.id = :userId ORDER BY spt.transactionTime DESC")
    Page<StudyPointsTransaction> findRecentTransactionsByUserId(@Param("userId") Long userId, Pageable pageable);
    
    @Query("SELECT spt FROM StudyPointsTransaction spt JOIN FETCH spt.user WHERE spt.user.id = :userId")
    Page<StudyPointsTransaction> findByUserIdWithUser(@Param("userId") Long userId, Pageable pageable);
}